package weibo.service;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import weibo.database.TweetDB;
import weibo.model.TweetMod;
import weibo.model.UserMod;
import weibo.tools.saveimage;

/**
 * Servlet implementation class createtweet
 */
@WebServlet("/createtweet")
public class createtweet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public createtweet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		//登录控制
		HttpSession session=request.getSession();
		List<UserMod> user=(List<UserMod>) session.getAttribute("user");
		if (user==null){
			response.setCharacterEncoding("utf8");
			response.setContentType("text/html;");
			response.getWriter().append("未登录，请点击 	 <a href='./userlogin'>登录</a>");
		}else{
			request.getRequestDispatcher("./index.html").forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		String Text=request.getParameter("text");
		String Img[] = new String[3];
		Img[0]=request.getParameter("img1");
		Img[1]=request.getParameter("img2");
		Img[2]=request.getParameter("img3");
		String image1URL=null;
		String image2URL=null;
		String image3URL=null;
		HttpSession session=request.getSession();
		List<UserMod> user=(List<UserMod>) session.getAttribute("user");
		if (user!=null){
			int userid=user.get(0).getUserid();
			//保存图片
			TweetDB db=new TweetDB();
			//查一下现在应该是多少张图片了
			int result=db.SelectTweetUserCount(userid);
			if (result!=-1){
				//第一张图片
				if(Img[0]!=""){
					String filename=userid+"_"+result+"_0_tweet.png";
					saveimage save=new saveimage(filename,Img[0]);
					//是否上传成功
					if (save.getFlag()){
						image1URL="./image?src="+filename;
					}
				}
				//第二张图片
				if(Img[1]!=""){
					String filename=userid+"_"+result+"_1_tweet.png";
					saveimage save=new saveimage(filename,Img[1]);
					//是否上传成功
					if (save.getFlag()){
						image2URL="./image?src="+filename;
					}
				}
				//第三张图片
				if(Img[2]!=""){
					String filename=userid+"_"+result+"_2_tweet.png";
					saveimage save=new saveimage(filename,Img[2]);
					//是否上传成功
					if (save.getFlag()){
						image3URL="./image?src="+filename;
					}
				}				
				//SQL语句插入
				TweetMod tweet=new TweetMod();
				tweet.TweetInsert(userid, Text, image1URL, image2URL, image3URL);
				boolean re=db.CreateTweet(tweet);
				if (re){
					System.out.println(user.get(0).getLogin_name()+" 用户发了一个推文");
					response.getWriter().append("success");
				}else{
					System.out.println(user.get(0).getLogin_name()+" 用户发了一个推文,但是失败了");
					response.getWriter().append("error");
				}
			}else{
				System.out.println(user.get(0).getLogin_name()+" 用户发推文失败,数据库原因");
				response.getWriter().append("error");
			}
			
			
		}else{
			System.out.println("未登录用户发推文");
			response.getWriter().append("error");
		}
//		System.out.println(Text);
//		System.out.println(Img[0]);
//		System.out.println(Img[1]);
//		System.out.println(Img[2]);
		
	}

}
